﻿//------------------------------------------------------------------------------
// ╭─────────────────────────────╮
// │ ╭─╮     ╭─╮              TM │   ╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣
// │ │ │     │ │                 │                                           ☺  
// │ │ ╰───╮ │ │ ╭─────╮ ╭─────╮ │     Name:Wedn.Net 数据库操作会话级对象   ♮ ♪ ♩
// │ │ ╭─╮ │ │ │ │ ╭─╮ │ │ ╭─╮ │ │                                    ♫ ♭      
// │ │ ╰─╯ │ │ │ │ ╰─╯ │ │ ╰─╯ │ │     Author:iceStone               ♬ ♪       
// │ └─────╯ └─╯ ╰─────╯ ╰───╮ │ │     Chinese:汪磊                              
// │                     ┌───╯ │ │                                              
// │                     ╰─────╯ │   ╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣
// ╰─────────────────────────────╯                                              
//------------------------------------------------------------------------------
using System.Data.Entity;
using System.Reflection;
using System.Runtime.Remoting.Messaging;
using Wedn.Net.IDAL;
using Wedn.Net.Model;
using Wedn.Net.Common;

namespace Wedn.Net.DALFactory
{
    /// <summary>
    /// 数据库操作会话级对象
    /// </summary>
    public class DbSession : IDbSession
    {
        private readonly string _path = Config.DALPath;
        private readonly string _nspace = string.IsNullOrEmpty(Config.DALNameSpace) ? Config.DALPath : Config.DALNameSpace;
        private readonly ICommentDAO _comment = null;
        /// <summary>
        /// CommentDAO 实例
        /// </summary>
		public ICommentDAO CommentDAO
        {
            get
			 {
				var className = _nspace + ".CommentDAO";
				return CreateInstance<ICommentDAO>(_comment, className);
			}
        }

        private readonly ILinkDAO _link = null;
        /// <summary>
        /// LinkDAO 实例
        /// </summary>
		public ILinkDAO LinkDAO
        {
            get
			 {
				var className = _nspace + ".LinkDAO";
				return CreateInstance<ILinkDAO>(_link, className);
			}
        }

        private readonly IOptionDAO _option = null;
        /// <summary>
        /// OptionDAO 实例
        /// </summary>
		public IOptionDAO OptionDAO
        {
            get
			 {
				var className = _nspace + ".OptionDAO";
				return CreateInstance<IOptionDAO>(_option, className);
			}
        }

        private readonly IPostDAO _post = null;
        /// <summary>
        /// PostDAO 实例
        /// </summary>
		public IPostDAO PostDAO
        {
            get
			 {
				var className = _nspace + ".PostDAO";
				return CreateInstance<IPostDAO>(_post, className);
			}
        }

        private readonly IPostMetaDAO _postmeta = null;
        /// <summary>
        /// PostMetaDAO 实例
        /// </summary>
		public IPostMetaDAO PostMetaDAO
        {
            get
			 {
				var className = _nspace + ".PostMetaDAO";
				return CreateInstance<IPostMetaDAO>(_postmeta, className);
			}
        }

        private readonly IRelationDAO _relation = null;
        /// <summary>
        /// RelationDAO 实例
        /// </summary>
		public IRelationDAO RelationDAO
        {
            get
			 {
				var className = _nspace + ".RelationDAO";
				return CreateInstance<IRelationDAO>(_relation, className);
			}
        }

        private readonly ITermDAO _term = null;
        /// <summary>
        /// TermDAO 实例
        /// </summary>
		public ITermDAO TermDAO
        {
            get
			 {
				var className = _nspace + ".TermDAO";
				return CreateInstance<ITermDAO>(_term, className);
			}
        }

        private readonly IUserDAO _user = null;
        /// <summary>
        /// UserDAO 实例
        /// </summary>
		public IUserDAO UserDAO
        {
            get
			 {
				var className = _nspace + ".UserDAO";
				return CreateInstance<IUserDAO>(_user, className);
			}
        }

        private readonly IUserMetaDAO _usermeta = null;
        /// <summary>
        /// UserMetaDAO 实例
        /// </summary>
		public IUserMetaDAO UserMetaDAO
        {
            get
			 {
				var className = _nspace + ".UserMetaDAO";
				return CreateInstance<IUserMetaDAO>(_usermeta, className);
			}
        }

		/// <summary>
        /// 实例化, 如果存在则返回对象, 不存在则返回null;
        /// </summary>
        /// <returns>泛型对象</returns>
        private T CreateInstance<T>(T instance, string className) where T : class
        {
            if (instance == null)
            {
                lock ("DbSessionLockHelper")
                {
                    instance = Assembly.Load(_path).CreateInstance(className) as T;
                }
            }
            return instance;
        }

		/// <summary>
        /// 保存数据库的改变状态
        /// </summary>
        /// <returns>受影响行数</returns>
        public int SaveChanges()
        {
            var dbContext = CallContext.GetData("CurrentDbContext") as DbContext;
            if (dbContext == null)
            {
                dbContext = new DataModelContainer();
                CallContext.SetData("CurrentDbContext", dbContext);
            }
            //var dbContext = DAL.Factory.GetCurrentDbContext();
            return dbContext.SaveChanges();
        }

		/// <summary>
        /// 获取当前线程内的DbSession对象；
        /// </summary>
		public static IDbSession Instance
        {
            get
            {
                var dbSession = CallContext.GetData("DbSession") as IDbSession;
                if (dbSession == null)
                {
                    dbSession = new DbSession();
                    CallContext.SetData("DbSession", dbSession);
                }
                return dbSession;
            }
        }
    }
} 
